﻿using System;
using System.Linq;
using System.Windows.Forms;
using ConfigurationManagement.Administration.Data;
using ConfigurationManagement.Data;
using Environment = ConfigurationManagement.Data.Environment;

namespace ConfigurationManagement.Administration.UI
{
    public partial class ManageEnvironmentsForm : Form
    {
        private readonly ConfigurationManagementDataContext _db = new ConfigurationManagementDataContext();
        private Project _selectedProject;

        public ManageEnvironmentsForm()
        {
            InitializeComponent();
        }

        private void ManageEnvironmentsFormLoad(object sender, EventArgs e)
        {
// ReSharper disable PossibleNullReferenceException
            EnvironmentsDataGridView.Columns["EnvironmentIdColumn"].DataPropertyName = "EnvironmentId";
// ReSharper restore PossibleNullReferenceException
// ReSharper disable PossibleNullReferenceException
            EnvironmentsDataGridView.Columns["EnvironmentNameColumn"].DataPropertyName = "EnvironmentName";
// ReSharper restore PossibleNullReferenceException
// ReSharper disable PossibleNullReferenceException
            EnvironmentsDataGridView.Columns["EnvironmentDEscriptionColumn"].DataPropertyName = "EnvironmentDEscription";
// ReSharper restore PossibleNullReferenceException
            EnvironmentsDataGridView.AutoGenerateColumns = false;

            ProjectsComboBox.DisplayMember = "ProjectName";
            ProjectsComboBox.DataSource = _db.Projects.OrderBy(x => x.ProjectName);
            if (ProjectsComboBox.Items.Count <= 0) return;
            ProjectsComboBoxSelectedIndexChanged(null, new EventArgs());
            RefreshEnvironments();
        }

        private void RefreshEnvironments()
        {
            EnvironmentsDataGridView.DataSource = _selectedProject == null ? null : _selectedProject.Environments;
        }

        private void ProjectsComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            SaveEnvironments();
            _selectedProject = null;
            if (string.IsNullOrEmpty(ProjectsComboBox.Text)) return;
            _selectedProject = _db.Projects.Single(p => p.ProjectName == ProjectsComboBox.Text);
            RefreshEnvironments();
        }

        private void ManageEnvironmentsFormFormClosing(object sender, FormClosingEventArgs e)
        {
            SaveEnvironments();
        }

        private void SaveEnvironments()
        {
            if (_selectedProject == null)
                return;

            if (!DataContext.ContainsChanges(_db)) return;
            var dr = MessageBox.Show(@"Changes have been made, would you like to submit them?",
                                              @"Confirm changes", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
                _db.SubmitChanges();
        }

        private void EnvironmentsDataGridViewUserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            var environment = (Environment) e.Row.DataBoundItem;
            if (environment.SettingValues.Count > 0)
            {
                var dr =
                    MessageBox.Show(@"This environment contains information, are you sure you wish to delete it?",
                                    @"Delete Environment", MessageBoxButtons.YesNo);
                if (dr != DialogResult.Yes)
                {
                    e.Cancel = true;
                    return;
                }
            }
            foreach (var settingValue in environment.SettingValues)
                _db.SettingValues.DeleteOnSubmit(settingValue);
        }
    }
}